package com.youlu.campus.web.data.english;


import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.VO.req.CheckMobileVO;
import com.youlu.campus.entity.english.vo.request.*;
import com.youlu.campus.service.english.*;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.user.service.UserService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class ActivityGroupController {

    @Autowired
    private ActivityGroupService activityGroupService;
    @Autowired
    private ActivityQaFeeService activityQaFeeService;
    @Autowired
    private UserCompetitionService userCompetitionService;

    @Autowired
    private CompetitionQaInfoService competitionQaInfoService;
    @Autowired
    private CompetitionFeeService competitionFeeService;

    @Autowired
    private UserCompetitionQaService userCompetitionQaService;

    @Autowired
    private UserService userService;


    /**
     * 获取报名信息
     * @return
     */
    @GetMapping("/mini-app/competition/userinfo")
    public QResult myUserInfo(){
        return QResult.success(userCompetitionService.findByUserId(RequestContext.getUserId()));
    }

    @PostMapping("/mini-app/competition/qa-my")
    public QResult qaMy(@RequestBody @Validated QueryQaHomeCommand queryQaHomeCommand,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
       queryQaHomeCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success( userCompetitionQaService.findByUserIdAndScore(queryQaHomeCommand));
    }

    @PostMapping("/mini-app/competition/qa-do")
    public QResult qaDo(@RequestBody @Validated DoQaCommand doQaCommand,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        doQaCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(competitionQaInfoService.doQa(doQaCommand));
    }

    @PostMapping("/mini-app/competition/qa-info")
    public QResult startQa(@RequestBody @Validated QueryQaCommand qaStartCommand,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaStartCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(competitionQaInfoService.findByQaInfoId(qaStartCommand));
    }

    @GetMapping("/mini-app/competition/score")
    public QResult myScore(){
        return QResult.success(userCompetitionQaService.myScore(RequestContext.getUserId()));
    }

    /**
     * 组别列表
     * @return
     */
    @GetMapping("/mini-app/competition/group")
    public QResult list(){
        return QResult.success(activityGroupService.home(RequestContext.getCurUser()));
    }

    /**
     * 费用列表
     * @param activityGroupId
     * @return
     */
    @GetMapping("/mini-app/competition/fee/detail")
    public QResult feeDetail(@RequestParam String activityGroupId){
        return QResult.success(activityQaFeeService.findById(RequestContext.getUserId(),activityGroupId));
    }


    /**
     * 费用单条
     * @param activityGroupId
     * @return
     */
    @GetMapping("/mini-app/competition/fee-one")
    public QResult feeByFeeTypeCode(@RequestParam String activityGroupId,@RequestParam String feeTypeCode){
        return QResult.success(activityQaFeeService.detail(activityGroupId,feeTypeCode));
    }

    /**
     * 报名
     * @param command
     * @return
     */
    @PostMapping("/mini-app/competition/register")
    public QResult userSignUp(@RequestBody @Validated CreateUserCompetitionCommand command, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        CheckMobileVO req = new CheckMobileVO();
        req.setUserPO(RequestContext.getCurUser());
        req.setCode(command.getCode());
        req.setMobile(command.getMobile());
        req.setPaltform(RequestContext.getCurPlatform());
        boolean re = userService.checkMobile(req);
        if (!re) {
            log.error(":>>> 用户验证码错误:{}", command.getCode());
            throw new BusinessException("验证码错误");
        }
        command.setCurrentUser(RequestContext.getCurUser());
        userCompetitionService.register(command);
        return QResult.success(null);
    }

    @PostMapping("/mini-app/competition/place-order")
    public QResult placeOrder(@RequestBody @Validated CompetitionPlaceOrderCommand command, HttpServletRequest request,
                              BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        command.setCurrentUser(RequestContext.getCurUser());
        command.setClientIp(request.getRemoteAddr());
        command.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(competitionFeeService.placeOrder(command));
    }

    @GetMapping("/mini-app/competition/order-status")
    public QResult orderStatus(@RequestParam String orderNo){
        return QResult.success(competitionFeeService.getStatusByOrderNo(orderNo));
    }
}
